package com.vmware.elastic.high;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

/**
 * @apiNote 过滤查询
 */
public class DocumentFilterQuery {
    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("10.192.193.98", 9200))
        );
        SearchRequest request = new SearchRequest("vmware");
        String[] includes = {"name","age"};//需要包含的字段
        String[] excludes = {};            //需要排除的字段
        request.source(
                new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).fetchSource(includes, excludes)//添加过滤条件
        );
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();//获取查询结果
        TimeValue took = response.getTook(); //获取响应时间
        System.out.println("响应时间:" + took);
        for (SearchHit hit : hits) {
            System.out.println(hit);
        }
        client.close();
    }
}
